﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Counter1
{
    /// <summary>
    /// 计算后缀表达式
    /// </summary>
    class Calculate
    {
        //存储字符串表达式
        private readonly string equation;

        //字符串数组
        private readonly List<string> element = new List<string>();

        //运算数栈
        private readonly Stack<double> number = new Stack<double>();

        //正无穷
        private const double infinity = 1.7E+308;

        public Calculate (string str)
        {
            equation = str;
        }
        
        /// <summary>
        /// 将字符串切割为字符串数组
        /// </summary>
        public void Incision()
        {
            string str = "";
            foreach(var index in equation)
            {
                if(char.IsDigit(index) || index == '.')
                {//index是数字或小数点
                    str += index;
                }
                else if(index == '+' || index == '-' || index == '*' || index == '/')
                {//index是运算符
                    if(str != "")
                    {
                        element.Add(str);
                    }
                    str = "" + index;
                    element.Add(str);
                    str = "";
                }
                else
                {//index是分隔符
                    if(str != "")
                    {
                        element.Add(str);
                        str = "";
                    }
                }
            }
            if(str != "")
            {
                element.Add(str);
            }
        }

        /// <summary>
        /// 读入后缀表达式并求值
        /// </summary>
        /// <returns>结果</returns>
        public double Getresult()
        {
            double num1 = 0 , num2 = 0;
            foreach(var item in element)
            {
                double num;
                if (double.TryParse(item, out num))
                {
                    number.Push(Double.Parse(item));
                }
                else
                {
                    if(number.Count != 0)
                    {
                        num2 = number.Pop();
                    }
                    else
                    {
                        num2 = infinity;
                        //Console.WriteLine("num2的值异常" );
                    }
                    if (number.Count != 0)
                    {
                        num1 = number.Pop();
                    }
                    else
                    {   
                        if(item == "*")
                        {
                            num1 = 1;
                        }
                        else if(item == "/")
                        {
                            Console.WriteLine("\n错误：除号前缺少运算数！");
                            Console.WriteLine("\n---------------------------------------------------------------------\n");
                            num2 = infinity;
                            //Console.WriteLine("num1的值异常");
                        }         
                    }

                    switch (item)
                    {
                        case "+": number.Push(num1 + num2);break;
                        case "-": number.Push(num1 - num2);break;
                        case "*": number.Push(num1 * num2);break;
                        case "/": 
                            if(num2 != 0.0)//检查除法的分母是否为零。
                            {
                                number.Push(num1 / num2);
                            }
                            else
                            {
                                Console.WriteLine("\n错误：分母不能为零！");
                                Console.WriteLine("\n---------------------------------------------------------------------\n");
                                num2 = infinity;
                            }
                            break;
                        default:
                            Console.WriteLine("\n错误：未知运算符！" + '>' + item + '<');
                            Console.WriteLine(equation);
                            num2 = infinity;
                            break;
                    }
                }
            }

            if(num2 < infinity)//如果处理完了表达式
            {
                if(number.Count != 0)//而此时堆栈正常
                {
                    num2 = number.Pop();//记录计算结果
                }
                else
                {
                    num2 = infinity;//否则标记错误
                    //Console.WriteLine("堆栈异常");
                }
            }

            return num2;
        }
    }
}
